package com.zto.security.browser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xingyuchao on 2017/12/17.
 */
@Component
public class MyUserDetailsService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登录用户名是：{}",username);

        //根据用户名查找用户信息  略  这个user是spring security默认的实现
        //return new User(username,"123456", AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));

        //根据查找的用户信息判断用户是否被冻结
//        return new User(username,"123456", false,true,true,true,AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));

        //处理密码加密解密
        String password = passwordEncoder.encode("123456");
        logger.info("用户密码是：{}",password);
        return new User(username,password, true,true,true,true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}